package com.sansecy.pagesnaprecyclerview;

import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.PagerSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.peceoqicka.gallerylayoutmanager.GalleryLayoutManager;

public class MainActivity extends AppCompatActivity {

    private MainActivity mActivity;
    private View mSnapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mActivity = this;
        final RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setAdapter(new MyAdapter());
        final PagerSnapHelper pageHelper = new PagerSnapHelper();
        pageHelper.attachToRecyclerView(recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.HORIZONTAL, false));
        final int dp30 = (int) ScreenUtils.convertDpToPixel(30, mActivity);
        final int dp60 = (int) ScreenUtils.convertDpToPixel(60, mActivity);
        final int maxMargin = (int) (ScreenUtils.getScreenWidth(this) * 0.11);
        final int minMargin = maxMargin / 2;
        final int halfMinMargin = minMargin / 2;
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                int position = parent.getChildAdapterPosition(view);
                if (position == 0) {
                    outRect.left = maxMargin;
                } else if (position == parent.getAdapter().getItemCount() - 1) {
                    outRect.right = maxMargin;
                } else if (position == parent.getAdapter().getItemCount() - 2) {
                    outRect.left = halfMinMargin;
                    outRect.right = minMargin;
                } else if (position == 1) {
                    outRect.left = minMargin;
                    outRect.right = halfMinMargin;
                } else {
                    outRect.left = halfMinMargin;
                    outRect.right = halfMinMargin;
                }
            }
        });
        recyclerView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                if (isFirst) {
                    isFirst = false;
                    mSnapView = pageHelper.findSnapView(recyclerView.getLayoutManager());
                    if (mSnapView != null) {
                        mSnapView.setScaleX(1.1f);
                        mSnapView.setScaleY(1.1f);
                    }
                }
            }
        });
        ViewTreeObserver viewTreeObserver = ((ViewGroup) recyclerView.getParent()).getViewTreeObserver();
        recyclerView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                return true;
            }
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                    count = true;
                    mSnapView = pageHelper.findSnapView(recyclerView.getLayoutManager());
                    int position = recyclerView.getChildAdapterPosition(mSnapView);
                    RecyclerView.ViewHolder leftViewHolder = recyclerView.findViewHolderForAdapterPosition(position - 1);
                    RecyclerView.ViewHolder rightViewHolder = recyclerView.findViewHolderForAdapterPosition(position - +1);
                    if (leftViewHolder != null) {
                        leftView = leftViewHolder.itemView;
                    }
                    if (rightViewHolder != null) {
                        rightView = rightViewHolder.itemView;
                    }
                } else if (newState == RecyclerView.SCROLL_STATE_SETTLING) {
                    count = true;
                } else if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    count = false;
                    offset = 0;
                    mSnapView = pageHelper.findSnapView(recyclerView.getLayoutManager());
                    int position = recyclerView.getChildAdapterPosition(mSnapView);
                    RecyclerView.ViewHolder leftViewHolder = recyclerView.findViewHolderForAdapterPosition(position - 1);
                    RecyclerView.ViewHolder rightViewHolder = recyclerView.findViewHolderForAdapterPosition(position +1);
                    if (leftViewHolder != null) {
                        leftView = leftViewHolder.itemView;
                    }
                    if (rightViewHolder != null) {
                        rightView = rightViewHolder.itemView;
                    }
                }
                Log.d(TAG, "onScrollStateChanged() called with: recyclerView = " + recyclerView.getChildCount());

                Log.d(TAG, "onScrollStateChanged: snapView " + mSnapView.getTag());
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                offset += dx;
                if (count) {

                }
                if (mSnapView != null) {
                    Log.d(TAG, "onScrolled: mSnapView.getLeft " + mSnapView.getLeft());
                }
                Log.d(TAG, "onScrolled:offset " + offset);
                if (dx > 0) {
                    if (mSnapView != null) {
                        mSnapView.setScaleX((float) (1 + 0.1f * ((1080f - offset) / 1080f)));
                        mSnapView.setScaleY((float) (1 + 0.1f * ((1080f - offset) / 1080f)));
                    }
//                    if (rightView != null) {
//                        rightView.setScaleY((float) (1 + 0.1f * (offset / 1080)));
//                        rightView.setScaleX((float) (1 + 0.1f * (offset / 1080)));
//                    }
                } else if (dx < 0) {
                    if (mSnapView != null) {
                        mSnapView.setScaleX((float) (1 + 0.1f * ((1080f - offset) / 1080f)));
                        mSnapView.setScaleY((float) (1 + 0.1f * ((1080f - offset) / 1080f)));
                    }
//                    if (leftView != null) {
//                        leftView.setScaleX((float) (1 + 0.1f * (offset / 1080)));
//                        leftView.setScaleY((float) (1 + 0.1f * (offset / 1080)));
//                    }
                }
            }
        });
    }

    boolean isFirst = true;
    boolean count = false;
    int offset;
    View leftView;
    View rightView;
    private static final String TAG = "MainActivity-App";
}
